package wangwenjun.phase1.flightquery.latch.v2;

import java.util.concurrent.TimeUnit;

/**
 * @author ChangLiang
 * @date 2020/6/16
 */
public abstract class LatchV2 {

    protected int limit;

    protected Runnable runnable;

    public Runnable getRunnable() {
        return runnable;
    }

    public LatchV2(int limit) {
        this.limit = limit;
    }

    public LatchV2(int limit, Runnable runnable) {
        this.limit = limit;
        this.runnable = runnable;
    }

    /**
     * 该方法会使得当前线程一直等待，直到所有的线程都完成工作，被阻塞的线程是允许被中断的
     * @throws InterruptedException
     */
    public abstract void await() throws InterruptedException;

    public abstract void await(TimeUnit unit, long time)
            throws InterruptedException, LatchWaitTimeoutException;

    /**
     * 当任务线程完成工作之后调用该方法使得计数器减－
     */
    public abstract void countDown();

    /**
     * 获取当前还有多少个线程没有完成任务
     * @return
     */
    public abstract int getUnArrived();
}
